import pymysql
from pymysql import cursors

class Database:
    @staticmethod
    def openDB(host, port, user, passwd, db ):
        dbobj=pymysql.connect(host=host,port=port,user=user,passwd=passwd,db=db,charset='utf8')
        cursor=dbobj.cursor(cursors.DictCursor)
        return dbobj,cursor
    @staticmethod
    def readALL(sql,cursor):
        cursor.execute(sql)
        record=cursor.fetchall()
        return record
    @staticmethod
    def readOne(sql,cursor):
        cursor.execute(sql)
        record = cursor.fetchone()
        return record
    @staticmethod
    def writeDB(spl,cursor):
        result=cursor.execute(spl)
        return result
    @staticmethod
    def closeDB(dbobj):
        dbobj.close()
if __name__ == '__main__':
    host='127.0.0.1'
    port=3306
    user='root'
    passwd='123456'
    db='students'

    dbobj,cursor=Database.openDB(host, port, user, passwd, db)
    sql="insert into students(sno,name,age,phone,address,sex) values('s001','张三',13,'1332333','china','男')," \
        "('s002','李四',16,'2222222','china','女'),('s003','王五',18,'3333333','china','男')"
    Database.writeDB(sql,cursor)
    dbobj.commit()
